package com.jweb.framework.core;

import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.support.DefaultMessageSourceResolvable;


public class CoreSuperException extends Exception implements MessageSourceResolvable {
	
	private static final long serialVersionUID = 8128574366773363023L;
	private final DefaultMessageSourceResolvable resolvable ;
	private boolean alreadyLogged ;
	
	public boolean isAlreadyLogged(){
		return this.alreadyLogged ;
	}

	public StackTraceElement[] getStackTrace(){
		
		this.alreadyLogged = true ;
		StackTraceElement[] st = super.getStackTrace() ;
		return st ;
	}
	
	public CoreSuperException(String code){
		super(getMessage(code , null)) ;
		this.resolvable = new DefaultMessageSourceResolvable(code) ;
	}
	
	public CoreSuperException(String code , Object[] arguments){
		super(getMessage(code , null)) ;
		this.resolvable = new DefaultMessageSourceResolvable(new String[]{code} , arguments) ;
	}
	
	public CoreSuperException(String code , Throwable throwable){
		super(getMessage(code, null) , throwable);
		this.resolvable = new DefaultMessageSourceResolvable(new String[]{code} , null , throwable.getMessage());
	}
	
	public CoreSuperException(String code , Object[] arguments , Throwable throwable){
		super(getMessage(code , null) , throwable) ;
		this.resolvable = new DefaultMessageSourceResolvable(new String[]{code} , arguments , throwable.getMessage());
	}
	
	public CoreSuperException(String code , String defaultMessage) {
		super(getMessage(code , defaultMessage));
		this.resolvable = new DefaultMessageSourceResolvable(new String[]{code} , null , defaultMessage) ;
	}
	
	public CoreSuperException(String code , Object[] arguments , String defaultMessage){
		super(getMessage(code , defaultMessage));
		this.resolvable = new DefaultMessageSourceResolvable(new String[]{code} , arguments , defaultMessage) ;
	}
	
	public CoreSuperException(String code , String defaultMessage , Throwable throwable) {
		super(getMessage(code, defaultMessage) , throwable) ;
		this.resolvable = new DefaultMessageSourceResolvable(new String[]{code} , new Object[]{throwable.getClass().getName()} , defaultMessage) ;
	}
	
	public CoreSuperException(String code , Object[] arguments , String defaultMessage , Throwable throwable){
		super(getMessage(code , defaultMessage) , throwable);
		this.resolvable = new DefaultMessageSourceResolvable(new String[]{code} , arguments , defaultMessage) ;
	}
	
	public String getCode(){
		return this.resolvable.getCode() ;
	}
	
	private static String getMessage(String code , String defaultMessage){
		if(code == null){
			return defaultMessage ;
		}
		if(defaultMessage == null){
			return "[" + code + "]" ;
		}
		
		return "[" + code + "]" + defaultMessage ;
	}
	
	public Object[] getArguments(){
		return this.resolvable.getArguments() ;
	}
	
	public String[] getCodes(){
		return this.resolvable.getCodes() ;
	}
	
	public String getDefaultMessage(){
		return this.resolvable.getDefaultMessage() ;
	}
}
